<?php
/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-09-20
 * 版    本：1.0.0
 * 功能说明：后台首页控制器。
 *
 **/

namespace Qwadmin\Controller;
use Think\Controller;

class AddmanagerController extends ComController
{
    /**
     * 支付路径指定 的 管理
     */
    public function pay_path()
    {
        $data = D('pay_path')->select();
        $pay_method = D('pay_path')->field('pay_method')->where('status=1')->find();
        $count = D('pay_path')->count();
        $this->assign('data', $data);
        $this->assign('pay_method', $pay_method);
        $this->assign('count', $count);
        $this->display('pay_index');
    }

    public function del()
    {

        $id = isset($_REQUEST['ids']) ? $_REQUEST['ids'] : false;
        if ($id) {
            if (is_array($id)) {
                $id = implode(',', $id);
                $map['id'] = array('in', $id);
            } else {
                $map = 'id=' . $id;
            }
            if (M('pay_path')->where($map)->delete()) {
                addlog('删除支付路径，AID：' . $id);
                $this->success('恭喜，支付路径删除成功！');
            } else {
                $this->error('参数错误！');
            }
        } else {
            $this->error('参数错误！');
        }

    }

    public function pay_edit($ids = 0)
    {
        $map_get['id'] = $ids;
        if (empty($_POST)) {
            $data = D('pay_path')->where($map_get)->find();
            $this->assign('data', $data);
            $this->display();
        } else {
            $map_post['pay_method'] = $_POST['pay_method'];
            $map_post['pay_path'] = $_POST['pay_path'];
            $map_id['id'] = $_POST['id'];
            $result = D('pay_path')->where($map_id)->save($map_post);
            if ($result) {
                $this->success('修改支付路径成功', 'pay_path', 2);
            } else {
                $this->error('修改支付路径失败', 'pay_path', 2);
            }
        }
    }

    public function pay_add()
    {
        if (empty($_POST)) {
            $data = D('pay_path')->select();
            $this->assign('data', $data);
            $this->display();
        } else {
            $pay_data['pay_method'] = $_POST['pay_method'];
            $pay_data['pay_path'] = $_POST['pay_path'];
            $pay_id = D('pay_path')->add($pay_data);
            if (!empty($pay_id)) {
                $this->success('添加支付路径成功!', 'pay_path', 2);
                die;
            }
            $this->error('添加支付路径失败!', 'pay_add', 2);
        }
    }

    public function pay_del($ids = 0)
    {
        $pay_data['id'] = $ids;
        $id = D('pay_path')->where($pay_data)->delete();
        if ($id) {
            $this->success('删除支付路径成功!');
            die;
        } else {
            $this->error('删除支付路径失败!');
            die;
        }
    }

    public function pay_choose()
    {
        if (empty($_POST)) {
            $data = D('pay_path')->select();
            $this->assign('data', $data);
            $this->display();
        } else {
            $id = $_POST['id'];
            $save2['status'] = 1;
            D()->execute('update qw_pay_path set status = 0');
            $res = D('pay_path')->where('id=' . $id)->save($save2);
            if ($res) {
                $this->success('设置支付成功!', 'pay_path');
                die;
            }
            $this->error('设置支付失败!', 'pay_path');

        }

    }
    //支付路径指定结束

    /**
     * 统计页面的管理
     */
    public function tongji()
    {
        $p = $_GET["p"] ? $_GET["p"] : 1;
        $map['is_service'] = 1;
        $map['is_stock'] = 0;
        $pagesize = 20;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量

        $count = D('platform')->where($map)->count();
        $data_platform = D('platform')->field('platform_id,platform_name,tongji_id')->where($map)->order("platform_id desc")->limit($offset . ',' . $pagesize)->select();
        $this->assign('count', $count);
        $this->assign('data_platform', $data_platform);
        $this->display('tongji_index');
    }

    public function tongji_edit()
    {
        if (empty($_POST)) {
            $tongji_data = D('platform')->field('tongji_id,platform_id,platform_name')->where('is_service = 1 and is_stock = 0')->select();
            $this->assign('tongji_data', $tongji_data);
            $this->display();
        } else {
            $platform_map['platform_id'] = $_POST['id'];
            $tongji_map['tongji_id'] = $_POST['tongji_id'];
            $pay_id = D('platform')->where($platform_map)->save($tongji_map);
            if (!empty($pay_id)) {
                $this->success('修改统计ID成功!', 'tongji', 2);
                die;
            }
            $this->error('修改统计ID失败!', 'tongji_edit', 2);
        }
    }

    //回调js
    public function tongji_edit_js()
    {
        $id = $_POST['id'];
        $tongji_id = D('platform')->where("platform_id=$id")->find();
        echo $tongji_id['tongji_id'];
    }


    /**
     * 落地域名的处理
     */

    //回显域名
    public function showdomain(){
        $showdomain = D('domain_bak')->select();
        $count = D('domain_bak')->count();
        $this->assign('count',$count);
        $this->assign('showdomain',$showdomain);
        $this->display();
    }

    //添加域名
    public function showdomain_add(){
        if (empty($_POST)) {
            $data = D('pay_path')->select();
            $this->assign('data', $data);
            $this->display();
        } else {
            $arr = explode("\r\n",$_POST['domain'][0]);
            $arr = array_filter($arr);
            foreach($arr as $v){
                $map['show_domain'] = trim($v);
                M('domain_bak')->data($map)->add();
            }
            $this->success('添加落地域名成功!','showdomain');
        }
    }

    //删除域名
    public function showdomain_del($id){
        $map['id'] = $id;
        $res = D('domain_bak')->where($map)->delete();
        if ($res){
            $this->success('删除域名成功');
        }else{
            $this->error('删除出现问题');
        }

    }
}